package com.bocai.freeman.view.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.bocai.freeman.R;
import com.bocai.freeman.utils.CommonUtils;
import com.bumptech.glide.Glide;

import java.util.List;

/**
 * Created by win7 on 2016/5/26.
 */
public class PerfectLightGridAdapter extends BaseAdapter {
    private Context mContext;
    private List<Integer> mList;
    /**
     * 是否加载图片，主要也是对滑动的优化
     */
    private boolean mIsLoadImage = true;
    private final int mImageWidth;

    public PerfectLightGridAdapter(Context mContext, List<Integer> list, boolean mIsLoadImage) {
        this.mContext = mContext;
        this.mList = list;
        this.mIsLoadImage = mIsLoadImage;
//        这里我曾经在加载的时候动态获取每个item的宽度在设置，但效果不理想。会出现测量不及时，使总高度变高，于是留下大片空白区域，但多滑动几次又正常了。
        mImageWidth = (CommonUtils.getScreenSizeWidth((Activity) mContext) - CommonUtils.dip2px(mContext, 102)) / 3;
    }

    @Override
    public int getCount() {
        if (mList != null) {
            return mList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(mContext, R.layout.item_perfect_light_grid, null);
            holder.iv_image = (ImageView) convertView.findViewById(R.id.iv_image);

            setImageParms(holder);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
//        if (mIsLoadImage) {
//            mImageLoader.displayImage(picBean.smallImageUrl, holder.iv_image, mConfig);
//        } else {
//            Bitmap bitmap = ImageUtils.getBitmapFromCache(picBean.smallImageUrl, mImageLoader);
//            if (bitmap != null) {
//                holder.iv_image.setImageBitmap(bitmap);
//            }
//        }
        Glide.with(mContext)
                .load(mList.get(position))
                .crossFade()
                .placeholder(R.mipmap.app_network_off)
                .into(holder.iv_image);
       holder.iv_image.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               Toast.makeText(mContext, "manyImage:" + position, Toast.LENGTH_SHORT).show();
           }
       });
        return convertView;
    }

    /**
     * 计算每个item的坐标
     * @param iv_image
     * @param list
     * @param position
     */
//    private void setupCoords(ImageView iv_image, List<Intent> list, int position) {
////        x方向的第几个
//        int xn=position%3+1;
////        y方向的第几个
//        int yn=position/3+1;
////        x方向的总间距
//        int h=(xn-1)*CommonUtils.dip2px(mContext,4);
////        y方向的总间距
//        int v=h;
////        图片宽高
//        int height = iv_image.getHeight();
//        int width = iv_image.getWidth();
////        获取当前点击图片在屏幕上的坐标
//        int[] points=new int[2];
//        iv_image.getLocationInWindow(points);
////        获取第一张图片的坐标
//        int x0=points[0]-(width+h)*(xn-1) ;
//        int y0=points[1]-(height+v)*(yn-1);
////        给所有图片添加坐标信息
//        for(int i=0;i<list.size();i++){
//            EaluationListBean.EaluationPicBean ealuationPicBean = mAttachmentsList.get(i);
//            ealuationPicBean.width=width;
//            ealuationPicBean.height=height;
//            ealuationPicBean.x=x0+(i%3)*(width+h);
//            ealuationPicBean.y=y0+(i/3)*(height+v)-CommonUtils.getStatusBarHeight(iv_image);
//        }
//    }

    /**
     * 设置图片的显示大小
     * @param holder
     */
    private void setImageParms(ViewHolder holder) {
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(mImageWidth, mImageWidth);
        holder.iv_image.setLayoutParams(params);
    }

    public boolean ismIsLoadImage() {
        return mIsLoadImage;
    }

    public void setmIsLoadImage(boolean mIsLoadImage) {
        this.mIsLoadImage = mIsLoadImage;
    }

    static class ViewHolder {
        public ImageView iv_image;
    }
}
